<template>
	<view class="details-view">
		<view class="details-tit">
			{{detailsForm.titleName}}
		</view>
		<view class="details-msg">
			<view class="author">
				作者：<text>{{detailsForm.author || '佚名'}}</text>
			</view>
			<view class="publish">
				发布：<text>{{detailsForm.releaseTime}}</text>
			</view>
		</view>
		<view class="richText" v-if="detailsForm.contents">
			<!-- <rich-text :nodes="detailsForm.contents"></rich-text> -->
			<u-parse :html="detailsForm.contents" :selectable="true" :tag-style="style"></u-parse>
		</view>
		<view class="richText" v-else>
			<!-- <rich-text :nodes="detailsForm.contents"></rich-text> -->
			<image :src="detailsForm.coverFileName" mode=""  @click="openPdf"></image>
		</view>
		<view class="look-over">
			<uni-icons type="eye" size="30"></uni-icons>
			<view class="look-tit" style="margin-right: 200rpx;">浏览量（{{detailsForm.pageViewNum||1}}）</view>
			<uni-icons type="star" size="30" :color="sc == 2 ? '#faa51b' : ''" v-if="sc == 1" @click="scang(2)"></uni-icons>
			<uni-icons type="star" size="30" :color="sc == 2 ? '#faa51b' : ''" v-else @click="scang(1)"></uni-icons>
			<view class="look-tit" style="margin-right: 40rpx;" v-if="sc == 1" @click="scang(2)">收藏</view>
			<view class="look-tit" style="margin-right: 40rpx;" v-else @click="scang(1)">已收藏</view>
			<!-- <uni-icons type="redo" size="30"></uni-icons> -->
			<!-- <view class="look-tit">
      </view> -->
      <view style="width: 140rpx;font-size: 24rpx;margin-top: -8rpx;">
        <button open-type="share" style="font-size: 24rpx;" >分享</button>
      </view>
      
		</view>
		<!-- <view style="margin: 20rpx 0;">相关推荐
		</view>
		<view class="recommend">
			<view class="recommend-item" v-for="item in list">
				<img class="recommend-img" src="../../static/img/tuijian.jpg" alt="">
				<view class="recommend-tit">
					糖尿病会造成急性或慢性并发症
				</view>
			</view>
		</view> -->
		<!-- <view class="review">
			<view class="review-top">
				<view class="review-left">
					评论（344）
				</view>
				<view class="review-icon">
					<uni-icons type="compose" size="30"></uni-icons>
				</view>
				<view class="review-right">
					参与评论
				</view>
			</view>
			<view class="review-list" v-for="i in 4">
				<img class="review-img" src="http://139.224.68.50:9017/file/18114b7680374a7b806e962aea7859b7.jpg"
					alt="">
				<view class="content">
					<view class="content-top">
						<view class="user-name">
							微信用户
						</view>
						<view class="review-time">
							2023-10-10
						</view>
					</view>
					<view class="review-content">
						金秋的阳光温馨安静，金秋的微风和煦温柔，金秋的蓝天白云飘逸，金秋的田野遍地金黄。在这个硕果累累的季节
					</view>
				</view>
			</view>
		</view> -->
	</view>
</template>

<script>
	import uParse from '../../components/u-parse/u-parse.vue'
	import * as topNewsList from '../../common/api/topNewsList.js'
	import * as me from '../../common/api/me.js'
	export default {
		components: {
			uParse
		},
		data() {
			return {
				uid: '',
				list: [1, 2, 3, 4],
				detailsForm: {
					titleName:'',
          pdfFileName:'',
				},
				style: {
					// 字符串的形式
					img: 'display: block;',
					video: 'display: block;width:670rpx;'
				},
				sc:1
			};
		},
		onLoad(e) {
			this.uid = e.uid
			this.getData()
		},
		methods: {
      openPdf(){
        let url=''
        if(this.detailsForm.pdfFileName){
          //转为https
          if(this.detailsForm.pdfFileName.indexOf("https")==-1){
            url=this.detailsForm.pdfFileName.replace("http","https");
          }else{
            url=this.detailsForm.pdfFileName
          }
          console.log(url,"--url--");
          this.downloadFileFunc(url)
        }
      },
      //打开pdf文件
      androidOpenPdf(url) {
        uni.showLoading({
          title: '正在打开...'
        })
        uni.downloadFile({
          url: url,
          success: function (res) {
            let filePath = res.tempFilePath;
            uni.openDocument({
              filePath: filePath,
              fileType:'pdf',
              success: function (res) {
                uni.hideLoading()
                console.log("打开pdf成功");
              },
            });
          },
        });
      },
      
      downloadFileFunc(url){
        let that=this;
              uni.getSystemInfo({
                  success: function(sysRes) {
                      if (sysRes.platform === 'ios') {
                          uni.navigateTo({
                            url:`/pages/webview/webview?url=${encodeURIComponent(url)}`
                          })
                      } else {
                        that.androidOpenPdf(url)
                      }
                  },
                  fail: function(err) {
                      uni.showToast({
                          title: '获取系统信息失败',
                          icon: 'none',
                          duration: 2000
                      });
                  }
                  })
              },
      
			// 收藏或已收藏
			scang(type){
				let param = {}
				if(type == 1){
					param = {
						uid:this.detailsForm.collectId
					};
				}else{
					param = {
						type:2,
						openId:uni.getStorageSync('openid_key'),
						targetId:this.uid
					};
				}
				
				me.collectInsert(param).then(res => {
					if(res.data.code == 200){
						this.sc = type
						this.getData();
					}
				})
			},
			getData() {
				let param = {
					uid: this.uid,
					openId:uni.getStorageSync('openid_key')
				}
				topNewsList.queryById(param).then(res => {
          if(res.data.data.contents){
            // http: 改为 https:
            res.data.data.contents = res.data.data.contents.replace(/http:/g, 'https:');
          }
					this.detailsForm = res.data.data
					this.sc = res.data.data.collectId == null ? 1 : 2;
				}).catch(error => {
					console.log(error)
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.details-view {
		padding: 50rpx 20rpx;

		.details-tit {
			text-align: center;
			font-size: 40rpx
		}

		.details-msg {
			display: flex;
			justify-content: space-between;
			font-size: 24rpx;
			margin-top: 30rpx;
		}

		.look-over {
			display: flex;
			font-size: 24rpx;
			line-height: 40rpx;
			margin: 20rpx 0;

			.look-tit {
				margin-bottom: 10rpx;
        width: 100rpx;
			}

			.uni-icons {
				font-size: 24rpx;
			}
		}

		.recommend {
			width: 710rpx;
			display: flex;
			gap: 40rpx;
			overflow-x: scroll;

			.recommend-item {
				width: 200rpx;

				.recommend-img {
					width: 200rpx;
					height: 140rpx;
					border-radius: 20rpx;
				}

				.recommend-tit {
					width: 200rpx;
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
				}
			}

		}

		.review {
			width: 710rpx;
			margin-top: 40rpx;

			.review-top {
				position: relative;
				height: 80rpx;

				.review-left {
					position: absolute;
					left: 0;
				}

				.review-icon {
					position: absolute;
					top: -16rpx;
					right: 160rpx;
				}

				.review-right {
					position: absolute;
					right: 0;
				}
			}

			.review-list {
				display: flex;
				gap: 20rpx;
				margin-bottom: 40rpx;

				.review-img {
					width: 80rpx;
					height: 80rpx;
					border-radius: 40rpx;
				}

				.content {
					width: 580rpx;

					.content-top {
						position: relative;
						height: 60rpx;
						width: 580rpx;

						.user-name {
							position: absolute;
							left: 0;
						}

						.review-time {
							position: absolute;
							right: 0;
						}
					}

					.review-content {
						display: -webkit-box;
						-webkit-line-clamp: 2;
						-webkit-box-orient: vertical;
						overflow: hidden;
					}
				}
			}
		}
	}

	.richText {
		margin: 20rpx;
		width: 670rpx;
	}

	video {
		display: block;
		margin: 20rpx auto;
		width: 710rpx;
	}
</style>